#!/usr/bin/ruby
require "rubygems"
require "opengl"
require "glut"
require "m3g"
require "pp"


class MyGLUTWidget
  def initialize
    GLUT.Init
    GLUT.InitWindowSize  256, 256
    GLUT.InitDisplayMode GLUT::GLUT_DOUBLE | GLUT::GLUT_RGB | GLUT::GLUT_DEPTH
    GLUT.CreateWindow "Ruby-M3G"

    initialize_scene

    GLUT.ReshapeFunc  method(:reshape).to_proc()
    GLUT.DisplayFunc  method(:display).to_proc()
    GLUT.KeyboardFunc method(:keyboard).to_proc()
    GLUT.MouseFunc    method(:mouse).to_proc()
    GLUT.IdleFunc     method(:idle).to_proc()
  end

  def initialize_scene

    controller = M3G::AnimationController.new 
    keyframe_fog_far = M3G::KeyframeSequence.new 3, 1, M3G::KeyframeSequence::LINEAR

    frame_fog_far = [[10], [4], [10]]
    keyframe_fog_far.keyframe[0] = 0,   frame_fog_far[0]
    keyframe_fog_far.keyframe[1] = 100, frame_fog_far[1]
    keyframe_fog_far.keyframe[2] = 200, frame_fog_far[2]
    keyframe_fog_far.repeat_mode = M3G::KeyframeSequence::LOOP
    keyframe_fog_far.valid_range = 0, 2
    keyframe_fog_far.duration    = 200

    animation_fog_far = M3G::AnimationTrack.new keyframe_fog_far, M3G::AnimationTrack::FAR_DISTANCE
    animation_fog_far.controller = controller

    positions = M3G::VertexArray.new  4, 3, 2
    positions.values[0,4] = [1,-1,0, 1,1,0, -1,-1,0, -1,1,0]

    scale = 1
    bias  = [0,0,0]
    vertices = M3G::VertexBuffer.new
    vertices.positions = positions, scale, bias
    vertices.default_color = 0x7f0000ff
    
    strips = [4]
    tris = M3G::TriangleStripArray.new 0, strips
    
    fog = M3G::Fog.new 
    fog.linear = 4, 10
    fog.color  = 0x000000ff
    fog.mode   = M3G::Fog::LINEAR
    fog.animation_track << animation_fog_far

    app = M3G::Appearance.new
    app.fog = fog

    mesh1 = M3G::Mesh.new vertices, tris, app
    mesh1.translate 0,0,-1

    mesh2 = M3G::Mesh.new vertices, tris, app
    mesh2.translate 0,0,-2

    mesh3 = M3G::Mesh.new vertices, tris, app
    mesh3.translate 0,0,-3

    mesh4 = M3G::Mesh.new vertices, tris, app
    mesh4.translate 0,0,-4

    mesh5 = M3G::Mesh.new vertices, tris, app
    mesh5.translate 0,0,-5

    grp = M3G::Group.new
    grp.child << mesh1
    grp.child << mesh2
    grp.child << mesh3
    grp.child << mesh4
    grp.child << mesh5

    cam = M3G::Camera.new
    cam.look_at 3,0,3, 0,0,-2, 0,1,0

    bg = M3G::Background.new
    bg.color = 0xff3f3f3f

    @wld = M3G::World.new
    @wld.child << cam
    @wld.child << grp
    @wld.active_camera = cam
    @wld.background    = bg


    @world_time = 0
  end

  def reshape width, height
    g3d = M3G::Graphics3D::instance
    g3d.viewport = 0,0,width,height
    @wld.active_camera.perspective = 45, width/(height*1.0), 0.1, 100
  end

  def display
    g3d = M3G::Graphics3D::instance
    g3d.render @wld
    GLUT.SwapBuffers()
  end
  
  def keyboard key, x, y
    if key == 113  # 'q'
      exit
    elsif key == 32 # ' '
      puts "main: time = #{@world_time}"
      @wld.animate @world_time
      @world_time += 2
    end
    GLUT.PostRedisplay
  end

  def mouse button, state, x, y
  end

  def idle
    GLUT.PostRedisplay
  end

  def start
    GLUT.MainLoop
  end

end


MyGLUTWidget.new.start

